package com.yff.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yff.dao.IUserDao;
import com.yff.model.User;
import com.yff.utils.DBUtils;



public class UserDaoImpl extends BaseDao implements IUserDao {
	//private BaseDao dao = new BaseDao();
	@Override
	public int add(User user) {
		String sql = "insert into tuser(username,password,roleid) values (?,?,?)";
		Object[] params = new Object[] {user.getUsername(),user.getPassword(),user.getRoleId()};
		return this.updateBySql(sql,params);
	}

	@Override
	public int delete(int id) {
		String sql = "delete from tuser where id=?";
		Object[] params = new Object[] {id};
		return this.updateBySql(sql,params);
	}

	@Override
	public int edit(User user) {
		String sql = "update tuser set username=?,password =? where id=?";
		Object[] params = new Object[] {user.getUsername(),user.getPassword(),user.getId()};
		return this.updateBySql(sql,params);
	}

	@Override
	public User get(int id) {
		String sql = "select * from tuser where id=?";
		User u = new User();
		Object[] params = new Object[] {id};
		queryBySql(sql,params);
		try {
			while(rs.next()) {
				
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setRoleId(rs.getInt("roleid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtils.close(conn, st, rs);
		}
		return u;
	}

	@Override
	public List<User> getAll() {
		String sql = "select * from tuser";
		List<User> list = new ArrayList<>();
		Object[] params = new Object[] {};
		queryBySql(sql,params);
		try {
			while(rs.next()) {
				User u = new User();
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setRoleId(rs.getInt("roleid"));
				list.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtils.close(conn, st, rs);
		}
		return list;
	}

	@Override
	public User login(String username, String password) {
		String sql = "select * from tuser where username=? and password =?";
		User u =null;
		Object[] params = new Object[] {username,password};
		queryBySql(sql,params);
		try {
			while(rs.next()) {
				u= new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setRoleId(rs.getInt("roleid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}

	@Override
	public boolean isExist(String username) {
		String sql = "select * from tuser where username=?";
		User u =null;
		Object[] params = new Object[] {username};
		queryBySql(sql,params);
		try {
			while(rs.next()) {
				u= new User();
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setRoleId(rs.getInt("roleid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtils.close(conn, st, rs);
		}
		return u==null?true:false;
	}

	@Override
	public List<User> getAll(int page, int pageSize) {
		int beginIndex=(page-1)*pageSize;
		String sql = "select * from tuser limit ?,?";
		List<User> list = new ArrayList<>();
		Object[] params = new Object[] {beginIndex,pageSize};
		queryBySql(sql,params);
		try {
			while(rs.next()) {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setRoleId(rs.getInt("roleid"));
				list.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int count(String sql, Object[] params) {
		queryBySql(sql,params);
		int count=-1;
		try {
			while(rs.next()) {
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

}

